﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using ATM_Pro.Module;

namespace ATM_Pro
{
    public class Configuration
    {
        private static Configuration instance = new Configuration();
        private string fileName = Properties.Settings.Default.AtmFileName;

        public Configuration()
        {
            InitAtmCollection();
        }

        //<!--ATM ID="0" IP="192.168.1.143" Address="0" Name="SouthGate">
        //  <Camera ID="0" Name="水平" />
        //  <Camera ID="1" Name="垂直" />
        //  <Camera ID="2" Name="全景" />
        //</ATM-->
        public static Configuration GetInstance()
        {
            return instance;
        }

        public List<ATM> Atms { get; set; }

        // initialize ATM collection
        private void InitAtmCollection()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(fileName);

            XmlNodeList atmList = xmlDoc.FirstChild.NextSibling.ChildNodes;
            List<ATM> listAtm = new List<ATM>();
            for (int i = 0; i < atmList.Count; i++)
            {
                XmlNode xnode = atmList[i];
                XmlNodeList camList = xnode.ChildNodes;
                ATM atm = new ATM();
                atm.ID = Convert.ToInt32(xnode.Attributes["ID"].Value);
                atm.AddName = xnode.Attributes["Name"].Value;
                atm.Ip = xnode.Attributes["IP"].Value.ToString();
                atm.AddId = Convert.ToInt32(xnode.Attributes["Address"].Value);
                List<Camera> listCamera = new List<Camera>();
                foreach (XmlNode cnode in camList)
                {
                    listCamera.Add(new Camera(Convert.ToInt32(cnode.Attributes["ID"].Value), cnode.Attributes["Name"].Value));
                }
                atm.Cams = listCamera;
                listAtm.Add(atm);
            }
            Atms = listAtm;
        }

        // save ATM collection to xml
        public void SaveAtmCollection()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            XmlNode root = doc.DocumentElement;
            root.RemoveAll();
            foreach (ATM atm in Atms)
            {
                XmlElement atmName = doc.CreateElement("ATM");

                atmName.Attributes.Append(doc.CreateAttribute("ID"));
                atmName.Attributes["ID"].Value = atm.ID.ToString();
                atmName.Attributes.Append(doc.CreateAttribute("IP"));
                atmName.Attributes["IP"].Value = atm.Ip;
                atmName.Attributes.Append(doc.CreateAttribute("Address"));
                atmName.Attributes["Address"].Value = atm.AddId.ToString();
                atmName.Attributes.Append(doc.CreateAttribute("Name"));
                atmName.Attributes["Name"].Value = atm.AddName.ToString();

                foreach (Camera cam in atm.Cams)
                {
                    XmlElement camName = doc.CreateElement("Camera");

                    camName.Attributes.Append(doc.CreateAttribute("ID"));
                    camName.Attributes["ID"].Value = cam.ID.ToString();
                    camName.Attributes.Append(doc.CreateAttribute("Name"));
                    camName.Attributes["Name"].Value = Enum.GetName(typeof(CameraNumber), cam.ID);

                    atmName.AppendChild(camName);
                }

                root.AppendChild(atmName);
            }
            doc.Save(fileName);
        }


    }
}
